package minderupt.selenate.core;

import com.thoughtworks.selenium.CommandProcessor;
import com.thoughtworks.selenium.DefaultSelenium;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.StringUtils;

import java.util.Calendar;

/**
 * Author:  Michael Dowling <michael.dowling@minderupt.com>
 */
public class SeleniumRunner extends DefaultSelenium {

    private static Log LOGGER = LogFactory.getLog(SeleniumRunner.class);

    private ExecutionResult executionResult;
    private TestCase currentlyRunningTestCase;
    private boolean testCaseFailure = false;


    public SeleniumRunner(String serverHost, int serverPort, String browserStartCommand, String browserURL) {
        super(serverHost, serverPort, browserStartCommand, browserURL);
        this.executionResult = new ExecutionResult();
    }

    public SeleniumRunner(CommandProcessor proc) {
        super(proc);
        this.executionResult = new ExecutionResult();
    }

    

    public void executeSeleniumCommand(String commandName, String...params) {



        if(this.testCaseFailure) {
            reportStepCompleted("NOT PERFORMED", "", commandName, params);
            return;
        }
        
        try {
            this.commandProcessor.doCommand(commandName, params);
            reportStepCompleted("OK", "", commandName, params);
        } catch(Exception e) {
            reportStepCompleted("FAILURE", e.getMessage(), commandName, params);
            this.testCaseFailure = true;
        }
        
    }

    public void reportTestCaseStarting(String testCaseName) {

        LOGGER.info("Starting test case:  " + testCaseName);
        start();
        TestCase tc = new TestCase(testCaseName);
        this.currentlyRunningTestCase = tc;
        this.testCaseFailure = false;
        this.currentlyRunningTestCase.setStart(Calendar.getInstance());

    }

    public void reportTestCaseEnding() {

        LOGGER.info("Ending test case: " + this.currentlyRunningTestCase.getName());
        this.currentlyRunningTestCase.setEnd(Calendar.getInstance());
        this.executionResult.getTestCases().add(this.currentlyRunningTestCase);
        this.currentlyRunningTestCase = null;
        stop();

    }

    public void reportStepCompleted(String status, String detailsOfStatus, String commandName, String...params) {
        TestStep step = new TestStep(status, detailsOfStatus, commandName, params);
        this.currentlyRunningTestCase.getTestSteps().add(step);
        LOGGER.info("\t" + commandName + " " + StringUtils.arrayToCommaDelimitedString(params) + " (" + status + ") " + detailsOfStatus);
    }

    public ExecutionResult getExecutionResult() {
        return(this.executionResult);
    }

    public void sleep(long millis) {
        try {
            LOGGER.info("\tsleep " + millis);
            Thread.sleep(millis);
        } catch(Exception e) {

        }
    }

    public String getValueOfElement(String locator) {

        String value = getValue(locator);
        return(value);


    }

    
    



}
